home *** CD-ROM | disk | FTP | other *** search
/ Aminet 28 / Aminet 28 (1998)(GTI - Schatztruhe)[!][Dec 1998].iso / Aminet / dev / misc / gms_dev.lha / GMSDev / Source / C / 3DObjects / PCalcSphere.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-05-07  |  5.0 KB  |  202 lines

  1. /* SAS/C: sc PCalcSphere.c opt math=standard INCDIR=INCLUDES:
  2. **
  3. ** This is a demo of a spinning sphere consisting entirely of dots.  The
  4. ** object is pre-calculated into a set of animations for maximum speed.
  5. **
  6. ** WARNING: This demo will take a long time before it begins, and you
  7. **          must have at least 512k of memory available.
  8. */
  9.  
  10. #include <proto/dpkernel.h>
  11. #include <system/debug.h>
  12. #include <math.h>
  13.  
  14. BYTE *ProgName      = "Spinning Sphere";
  15. BYTE *ProgAuthor    = "Paul Manias";
  16. BYTE *ProgDate      = "February 1998";
  17. BYTE *ProgCopyright = "DreamWorld Productions (c) 1996-1998.  Freely distributable.";
  18. BYTE *ProgShort     = "3-Dimensional object demonstration.";
  19.  
  20. struct GScreen *screen;
  21. struct JoyData *jport;
  22.  
  23. void Demo(void);
  24. void CalcFrames(void);
  25.  
  26. #define AMTDOTS 500       /* The amount of dots in the object. */
  27. #define FRAMES  90
  28. #define ANGLE   (360/FRAMES)
  29.  
  30. struct DotPixel { double X,Y,Z; };
  31.  
  32. LONG palette[] = {
  33.   PALETTE_ARRAY,32,
  34.   0x000000L,0x101010L,0x171717L,0x202020L,0x272727L,0x303030L,0x373737L,0x404040L,
  35.   0x474747L,0x505050L,0x575757L,0x606060L,0x676767L,0x707070L,0x777777L,0x808080L,
  36.   0x878787L,0x909090L,0x979797L,0xa0a0a0L,0xa7a7a7L,0xb0b0b0L,0xb7b7b7L,0xc0c0c0L,
  37.   0xc7c7c7L,0xd0d0d0L,0xd7d7d7L,0xe0e0e0L,0xe0e0e0L,0xf0f0f0L,0xf7f7f7L,0xffffffL
  38. };
  39.  
  40. struct PixelEntry *lists[FRAMES];
  41.  
  42. struct PixelList PixelList = {
  43.   AMTDOTS,
  44.   sizeof(struct PixelEntry),
  45.   NULL
  46. };
  47.  
  48. /***********************************************************************************/
  49.  
  50. void main(void)
  51. {
  52.   WORD i;
  53.  
  54.   if (screen = InitTags(NULL,
  55.      TAGS_SCREEN,    NULL,
  56.      GSA_Attrib,     SCR_DBLBUFFER,
  57.        GSA_BitmapTags, NULL,
  58.        BMA_Palette,    palette,
  59.        TAGEND, NULL,
  60.      TAGEND)) {
  61.  
  62.      if (jport = Init(Get(ID_JOYDATA),NULL)) {
  63.         for (i=0; i < FRAMES; i++) {
  64.            lists[i] = AllocMemBlock(sizeof(struct PixelEntry)*AMTDOTS,MEM_DATA);
  65.         }
  66.         PixelList.Pixels = lists[i];
  67.  
  68.         CalcFrames();
  69.  
  70.         Demo();
  71.  
  72.         for (i=0; i < FRAMES; i++) {
  73.            FreeMemBlock(lists[i]);
  74.         }
  75.  
  76.      Free(jport);
  77.      }
  78.   Free(screen);
  79.   }
  80. }
  81.  
  82. /************************************************************************************
  83. **
  84. */
  85.  
  86. void Demo(void)
  87. {
  88.   WORD i=NULL;
  89.  
  90.   Display(screen);
  91.  
  92.   do {
  93.     Query(jport);
  94.     Clear(screen->Bitmap);
  95.  
  96.     PixelList.Pixels = lists[i];
  97.  
  98.     DrawPixelList(screen->Bitmap,&PixelList);
  99.  
  100.     i++;
  101.     if (i >= FRAMES) {
  102.        i = NULL;
  103.     }
  104.  
  105.     WaitAVBL();
  106.     SwapBuffers(screen);
  107.   } while(!(jport->Buttons & JD_LMB));
  108. }
  109.  
  110. /************************************************************************************
  111. ** Longtitude deterimines the position of the dot on the horizontal axis.
  112. ** Latitude determines the position of the dot on the vertical axis.
  113. */
  114.  
  115. void CalcFrames(void)
  116. {
  117.   struct DotPixel *object=NULL; /* Pointer to our 3D object */
  118.   double *sine=NULL;            /* Pointer to our sine table */
  119.   double *cosine=NULL;          /* Pointer to our cosine table */
  120.   WORD   i,j;
  121.   WORD   offsetx = (screen->Width/2);
  122.   WORD   offsety = (screen->Height/2);
  123.   double angle   = 0;
  124.   LONG   scale   = 60;
  125.   UWORD  anglex  = 0, angley = 0, anglez = 0;
  126.   double temp;
  127.   ULONG  colour;
  128.   double Z2,X2,Y2;
  129.   double longtitude;
  130.   double latitude;
  131.   struct PixelEntry *Entry;
  132.  
  133.   DMsg("Allocating 3D calculation memory.");
  134.  
  135.   if (object = AllocMemBlock(sizeof(struct DotPixel)*AMTDOTS,MEM_DATA)) {
  136.    if (sine   = AllocMemBlock(sizeof(double)*360,MEM_DATA)) {
  137.     if (cosine = AllocMemBlock(sizeof(double)*360,MEM_DATA)) {
  138.  
  139.      /* First calculate the X, Y and Z coordinates of our object */
  140.  
  141.      DMsg("Calculating the object's coordinates.");
  142.  
  143.      for (i = 0; i < AMTDOTS; i++) {
  144.        longtitude = FastRandom(100);
  145.        latitude   = FastRandom(100);
  146.        object[i].X  = sin(longtitude)*cos(latitude);
  147.        object[i].Y  = sin(longtitude)*sin(latitude);
  148.        object[i].Z  = cos(longtitude);
  149.      }
  150.  
  151.      DMsg("Generating sine and cosine tables.");
  152.  
  153.      /* Now generate our cosine and sinus tables */
  154.  
  155.      for (i = 0; i < 360; i++) {
  156.        cosine[i] = cos(angle);
  157.        sine[i]   = sin(angle);
  158.        angle    += 0.25;
  159.      }
  160.  
  161.      /* Do our big pre-calculation loop */
  162.  
  163.      DMsg("Big calculation loop is starting now...");
  164.  
  165.      for (j = 0; j < FRAMES; j++) {
  166.  
  167.        Entry = lists[j];
  168.  
  169.        DPrintF("Frame:","%d",j);
  170.  
  171.        for (i = 0; i < AMTDOTS; i++) {
  172.          X2 = object[i].X;
  173.          Y2 = object[i].Y;
  174.          Z2 = object[i].Z;
  175.   
  176.          temp = Z2;
  177.          Z2 = (Z2 * cosine[anglex]) - (Y2 * sine[anglex]);
  178.          Y2 = (Y2 * cosine[anglex]) + (temp * sine[anglex]);
  179.  
  180.          colour = ((Z2+1)*screen->Bitmap->AmtColours)/2;
  181.  
  182.          X2 *= scale;
  183.          Y2 *= scale;
  184.  
  185.          Entry->XCoord = X2+offsetx;
  186.          Entry->YCoord = Y2+offsety;
  187.          Entry->Colour = colour;
  188.          Entry++;
  189.        }
  190.        anglex += ANGLE; if (anglex >= 360) anglex = 0;
  191.        angley += ANGLE; if (angley >= 360) angley = 0;
  192.        anglez += ANGLE; if (anglez >= 360) anglez = 0;
  193.      }
  194.     FreeMemBlock(cosine);
  195.     }
  196.    FreeMemBlock(sine);
  197.    }
  198.   FreeMemBlock(object);
  199.   }
  200. }
  201.  
  202.